package com.lock.subscribe.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 

public class AESUtils {
	private static final String AES_TYPE = "AES/ECB/PKCS5Padding";
    //加密
    public static String Encrypt(String content,String KEY,String IV) throws Exception {  
        byte[] raw = KEY.getBytes("utf-8");  
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
        Cipher cipher = Cipher.getInstance(AES_TYPE);//"算法/模式/补码方式"
        //使用CBC模式，需要一个向量iv，可增加加密算法的强度    
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);  
        byte[] encrypted = cipher.doFinal(content.getBytes());  
        return  Base64Util.encode(encrypted);
    }  
	
    //解密  
    public static String Decrypt(String content,String KEY,String IV) throws Exception {  
        try {   
            byte[] raw = KEY.getBytes("utf-8");  
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
            Cipher cipher = Cipher.getInstance(AES_TYPE);  
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());  
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);  
            byte[] encrypted1 = Base64Util.decode(content);
            try {  
                byte[] original = cipher.doFinal(encrypted1);  
                String originalString = new String(original);  
                return originalString;  
            } catch (Exception e) {  
                System.out.println(e.toString());  
                return null;  
            }  
        } catch (Exception ex) {  
            System.out.println(ex.toString());  
            return null;  
        }  
    } 
    public static void main(String[] args) throws Exception {

}
}